package com.lwl.leetcode;

import org.junit.Test;

import java.util.*;

/**
 * @author lwl
 * @Description 数字 n 代表生成括号的对数，请你设计一个函数，用于能够生成所有可能的并且 有效的 括号组合。
 * @date 2023/7/28 15:16
 */
public class Code22GenerateParenthesis {
    public List<String> generateParenthesis(int n) {
        if (n <= 0) {
            return new ArrayList<>();
        }
        char[] str = new char[n * 2];
        ArrayList<String> res = new ArrayList<>();
        process(str, 0, 0, 0, res);
        return res;
    }

    private void process(char[] str, int index, int curLeftCount, int totalLeftCount, ArrayList<String> res) {
        if (index == str.length) {
            res.add(new String(str));
            return;
        }
        if (curLeftCount > 0) {
            str[index] = ')';
            process(str, index + 1, curLeftCount - 1, totalLeftCount, res);
        }
        if (totalLeftCount < str.length / 2) {
            str[index] = '(';
            process(str, index + 1, curLeftCount + 1, totalLeftCount + 1, res);
        }
    }

    @Test
    public void test() {
        List<String> strings = generateParenthesis(6);
        strings.forEach(System.out::println);
    }

    @Test
    public void test2() {
        Map<String, String> map = new HashMap<>();
        Hashtable<String, String> hashtable = new Hashtable<>();
        hashtable.put("null", null);
        map.put("a1", "b1");
        map.put("a2", "b2");
        map.replace("a1", null);
        map.replace("a2", null);
//        map.replace("a1", "999");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }

}
